package com.jic.veteran.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.jic.veteran.common.service.IdService;
import com.jic.veteran.common.util.GenerateUtil;
import com.jic.veteran.component.VeteranSecurityContext;
import com.jic.veteran.dao.DonatePayDetailDao;
import com.jic.veteran.dto.ProjectBasicQueryParam;
import com.jic.veteran.dto.ProjectDto;
import com.jic.veteran.mapper.ProjectBasicMapper;
import com.jic.veteran.mapper.ProjectImplementMapper;
import com.jic.veteran.model.ProjectBasic;
import com.jic.veteran.model.ProjectBasicExample;
import com.jic.veteran.model.ProjectImplement;
import com.jic.veteran.model.ProjectImplementExample;
import com.jic.veteran.service.ProjectService;
import com.jic.veteran.service.UmsAdminCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectServiceImpl.class);

    @Autowired
    private ProjectBasicMapper projectBasicMapper;
    @Autowired
    private ProjectImplementMapper projectImplementMapper;
    @Autowired
    private UmsAdminCacheService umsAdminCacheService;
    @Autowired
    private DonatePayDetailDao donatePayDetailDao;
    @Autowired
    private IdService idService;

    @Override
    public List<ProjectBasic> listAll() {
        return projectBasicMapper.selectByExample(new ProjectBasicExample());
    }

    @Override
    public List<ProjectBasic> list(ProjectBasicQueryParam projectBasicQueryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        ProjectBasicExample projectBasicExample = new ProjectBasicExample();
        if(StrUtil.isNotEmpty(projectBasicQueryParam.getProjectId())) {
            projectBasicExample.createCriteria().andProjectIdEqualTo(projectBasicQueryParam.getProjectId());
        }
        if(StrUtil.isNotEmpty(projectBasicQueryParam.getProjectName())) {
            projectBasicExample.createCriteria().andProjectNameLike("%" + projectBasicQueryParam.getProjectName() + "%");
        }
        if(NumberUtil.isNumber(projectBasicQueryParam.getProjectStatus())) {
            projectBasicExample.createCriteria().andProjectStatusEqualTo(Integer.valueOf(projectBasicQueryParam.getProjectStatus()));
        }
        if(StrUtil.isNotEmpty(projectBasicQueryParam.getCreateTime())) {
            projectBasicExample.createCriteria()
                    .andStartTimeLessThanOrEqualTo(DateUtil.parse(projectBasicQueryParam.getCreateTime()))
                    .andEndTimeGreaterThanOrEqualTo(DateUtil.parse(projectBasicQueryParam.getCreateTime()));
        }
        return projectBasicMapper.selectByExampleWithBLOBs(projectBasicExample);
    }

    @Override
    public int create(ProjectBasic projectBasic, ProjectImplement projectImplement) {
        projectBasic.setProjectStatus(0);
        projectBasic.setProjectId("P"+DateUtil.format(DateUtil.date(),"yyyyMMdd")+StrUtil.fillBefore(idService.generateProjectId(),'0',9));
        projectBasic.setCreateBy(VeteranSecurityContext.getUserName());
        projectBasic.setCreateTime(DateUtil.date());
        projectBasic.setModifyBy(VeteranSecurityContext.getUserName());
        projectBasic.setModifyTime(DateUtil.date());
        projectBasicMapper.insert(projectBasic);
        projectImplement.setProjectId(projectBasic.getProjectId());
        projectImplement.setProjectName(projectBasic.getProjectName());
        projectImplement.setCreateBy(VeteranSecurityContext.getUserName());
        projectImplement.setCreateTime(DateUtil.date());
        projectImplement.setModifyBy(VeteranSecurityContext.getUserName());
        projectImplement.setModifyTime(DateUtil.date());
        return projectImplementMapper.insert(projectImplement);
    }

    @Override
    public int update(Long id,ProjectDto projectDto) {
        int result = 0;
        ProjectBasic projectBasic = projectBasicMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNotEmpty(projectBasic)) {
            ProjectBasic projectBasicUpdate = projectDto .getProjectBasic();
            projectBasicUpdate.setId(id);
            projectBasicUpdate.setModifyBy(VeteranSecurityContext.getUserName());
            projectBasicUpdate.setModifyTime(DateUtil.date());
            result = projectBasicMapper.updateByPrimaryKeySelective(projectBasicUpdate);
            if(result > 0) {
                ProjectImplementExample example = new ProjectImplementExample();
                example.createCriteria().andProjectIdEqualTo(projectBasic.getProjectId());
                List<ProjectImplement> projectImplementList = projectImplementMapper.selectByExampleWithBLOBs(example);
                if(!projectImplementList.isEmpty()) {
                    ProjectImplement projectImplement = projectDto.getProjectImplement();
                    projectImplement.setId(projectImplementList.get(0).getId());
                    projectImplement.setModifyBy(VeteranSecurityContext.getUserName());
                    projectImplement.setModifyTime(DateUtil.date());
                    return projectImplementMapper.updateByPrimaryKeySelective(projectImplement);
                }
            }
        }
        return result;
    }

    @Override
    public int release(Long id) {
        ProjectBasicExample projectBasicExample = new ProjectBasicExample();
        projectBasicExample.createCriteria().andIdEqualTo(id);
        projectBasicExample.createCriteria().andProjectStatusEqualTo(1);
        List<ProjectBasic> projectBasicList = projectBasicMapper.selectByExample(projectBasicExample);
        if(null != projectBasicList && projectBasicList.size() == 1) {
            ProjectBasic projectBasic = projectBasicList.get(0);
            projectBasic.setProjectStatus(1);
            umsAdminCacheService.setProjectDtoList(projectBasic);
        }
        ProjectBasic projectBasic = projectBasicMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNotEmpty(projectBasic) && !StrUtil.equals(String.valueOf(projectBasic.getProjectStatus()),"1")) {
//            projectBasic.setModifyBy(VeteranSecurityContext.getUserName());
            projectBasic.setModifyTime(DateUtil.date());
            projectBasic.setProjectStatus(1);
            int flag = projectBasicMapper.updateByPrimaryKeySelective(projectBasic);
            if(flag > 0) {
                ProjectImplementExample example = new ProjectImplementExample();
                example.createCriteria().andProjectIdEqualTo(projectBasic.getProjectId());
                List<ProjectImplement> projectImplementList = projectImplementMapper.selectByExampleWithBLOBs(example);
                if(!projectImplementList.isEmpty()) {
                    ProjectDto projectDto = new ProjectDto();
                    projectDto.setProjectBasic(projectBasic);
                    projectDto.setProjectImplement(projectImplementList.get(0));
                    umsAdminCacheService.setProject(projectDto);
                    return flag;
                }
            }
        }

        return 0;
    }

    @Override
    public int delete(Long id) {
        ProjectBasicExample projectBasicExample = new ProjectBasicExample();
        projectBasicExample.createCriteria().andIdEqualTo(id);
        List<ProjectBasic> projectBasicList = projectBasicMapper.selectByExample(projectBasicExample);
        if(null != projectBasicList && projectBasicList.size() == 1) {
            ProjectBasic projectBasic = projectBasicList.get(0);
            projectBasic.setProjectStatus(1);
            umsAdminCacheService.delProject(projectBasic);
            umsAdminCacheService.removeProjectFromList(projectBasic);
        }
        int result = 0;
        ProjectBasic projectBasic = projectBasicMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNotEmpty(projectBasic)) {
            if(result >0) {
                ProjectImplementExample example = new ProjectImplementExample();
                example.createCriteria().andProjectIdEqualTo(projectBasic.getProjectId());
                List<ProjectImplement> projectImplementList = projectImplementMapper.selectByExampleWithBLOBs(example);
                if(!projectImplementList.isEmpty()) {
                    ProjectImplementExample projectImplementExample = new ProjectImplementExample();
                    projectImplementExample.createCriteria().andProjectIdEqualTo(projectBasic.getProjectId());
                    result = projectImplementMapper.deleteByExample(projectImplementExample);
                    return result;
                }
            }
        }
        return result;
    }

    @Override
    public int stop(Long id) {
        ProjectBasicExample projectBasicExample = new ProjectBasicExample();
        projectBasicExample.createCriteria().andIdEqualTo(id);
        projectBasicExample.createCriteria().andProjectStatusEqualTo(1);
        List<ProjectBasic> projectBasicList = projectBasicMapper.selectByExample(projectBasicExample);
        if(null != projectBasicList && projectBasicList.size() == 1) {
            umsAdminCacheService.delProject(projectBasicList.get(0));
            umsAdminCacheService.removeProjectFromList(projectBasicList.get(0));
        }
        ProjectBasic projectBasic = projectBasicMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNotEmpty(projectBasic) && StrUtil.equals(String.valueOf(projectBasic.getProjectStatus()),"1")) {
            //projectBasic.setModifyBy(VeteranSecurityContext.getUserName());
            projectBasic.setModifyTime(DateUtil.date());
            projectBasic.setProjectStatus(2);
            int result = projectBasicMapper.updateByPrimaryKeySelective(projectBasic);
            return result;
        }
        return 0;
    }

    @Override
    public ProjectDto getItem(Long id) {
        ProjectBasicExample projectBasicExample = new ProjectBasicExample();
        projectBasicExample.createCriteria().andIdEqualTo(id);
        List<ProjectBasic> projectBasicList = projectBasicMapper.selectByExampleWithBLOBs(projectBasicExample);
        if(!projectBasicList.isEmpty()) {
            ProjectBasic projectBasic = projectBasicList.get(0);
            ProjectDto projectDto = new ProjectDto();
            projectDto.setProjectBasic(projectBasic);
            ProjectImplementExample projectImplementExample = new ProjectImplementExample();
            projectImplementExample.createCriteria().andProjectIdEqualTo(projectBasic.getProjectId());
            List<ProjectImplement> projectImplements = projectImplementMapper.selectByExampleWithBLOBs(projectImplementExample);
            if (!projectImplements.isEmpty()) {
                projectDto.setProjectImplement(projectImplements.get(0));
            }
            return projectDto;
        }
        return null;
    }

    @Override
    public JSONObject getDonateData(String startDate, String endDate) {
        List<Map> sumData = donatePayDetailDao.getSumDataByDate(startDate, endDate);
        String maxAmt = donatePayDetailDao.getMaxAmt(startDate, endDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("maxAmt",maxAmt);
        jsonObject.set("sumData",sumData);
        return jsonObject;
    }
}
